package com.homework.HomeAcount.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.homework.HomeAcount.dao.MenuDao;
import com.homework.HomeAcount.entity.Menu;
import com.homework.HomeAcount.entity.TreeNode;
import com.homework.HomeAcount.service.MenuService;

@Service("menuService")
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao menuDao;

	@Override
	public List<Menu> findMenusByUserId(String userId) {
		return menuDao.findMenusByUserId(userId);
	}

	@Override
	public List<Menu> findSubMenusByParentId(String menuId) {
		return menuDao.findSubMenusByParentId(menuId);
	}

	@Override
	public List<TreeNode> getMenus(Map<String, Object> map) {
		List<Menu> list = menuDao.getMenus(map);
		if(null != list && list.size() > 0) {
			List<TreeNode> nodeList = new ArrayList<TreeNode>();
			for(int i = 0; i < list.size(); i++) {
				TreeNode treeNode = new TreeNode();
				Menu menu = list.get(i);
				treeNode.setId(menu.getId());
				treeNode.setName(menu.getName());
				treeNode.setpId(menu.getParentid());
				treeNode.setIsParent(menu.getIsParent().equals("1")? true : false);
				nodeList.add(treeNode);
			}
			return nodeList;
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getParentMenus() {
		return menuDao.getParentMenus();
	}

	@Override
	public Menu getMenuInfoById(String id) {
		return menuDao.getMenuInfoById(id);
	}

	@Override
	public void updateMenuById(Menu menu) throws Exception {
		menuDao.updateMenuById(menu);
	}

	@Override
	public void insertMenuInfo(Menu menu) throws Exception {
		menuDao.insertMenuInfo(menu);
	}
	
}
